package com.eden.door.listener;

import java.awt.Cursor;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.ImageIcon;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 * 表格鼠标监听器
 * 主要监听点击删除按钮时的操作
 * @author Eden
 *
 */
public class TableMouseListener implements MouseListener ,MouseMotionListener{

	private JTable table ;
	public TableMouseListener(JTable table){
		this.table = table ;
	}
	
	/**
	 * 处理鼠标的点击事件
	 * 点击删除按钮时,移除所在行,刷新序号
	 */
	@Override
	public void mouseClicked(MouseEvent e) {
		Point p = e.getPoint() ;
		int row = table.rowAtPoint(p) ;
		int column = table.columnAtPoint(p) ;
		int columnCount = table.getColumnCount();
		if(column == columnCount -1 ){
			value = table.getValueAt(row, column) ;
			if(value!=null){
				refreshRowNum(this.table , row ) ;
				((DefaultTableModel)table.getModel()).removeRow(row) ;
				
			}
		}
	}

	
	/**
	 * 刷新表下单序号
	 * @param table 操作的表
	 * @param row 删除的号的行号
	 */
	public void refreshRowNum(JTable table , int row ){
		int rowCount = table.getRowCount() ;
		if(rowCount - 10 < 1) return ;
		int rowNum = ((Integer) table.getValueAt(row, 0)) - 1 ;
		
		for(int i = row ; i < rowCount -  10 ; i ++) {
			table.setValueAt(rowNum++, i , 0) ;
		}
	}
	
	@Override
	public void mousePressed(MouseEvent e) {

	}

	@Override
	public void mouseReleased(MouseEvent e) {

	}

	@Override
	public void mouseEntered(MouseEvent e) {
	
	}

	@Override
	public void mouseExited(MouseEvent e) {
	}
	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	ImageIcon l = new ImageIcon("image/rowdel.png");
	ImageIcon h = new ImageIcon("image/rowdelh.png");
	Object value ;
	
	boolean isHighLight = false ;
	
	
	int rowOver = -1 , columnOver ;
	@Override
	public void mouseMoved(MouseEvent e) {
		Point p = e.getPoint() ;
		int row = table.rowAtPoint(p) ;
		int column = table.columnAtPoint(p) ;
		int columnCount = table.getColumnCount();
		int rowNum = table.getRowCount();
		
		
	/*	for(int i=0 ; i < rowNum ; i++){
//			value = table.getValueAt(i, column) ;
			if(rowNum - 10 > 0){
				if(isHighLight){
					isHighLight = false ;
					table.setValueAt(l, i, columnCount-1);
					table.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)) ;
				}
			}
		}*/
		if(column == columnCount -1 ){
//			value = table.getValueAt(row, column) ;
			if(rowNum - 10 > row){
			/*	if(rowOver != row && rowOver != -1) {
					table.setValueAt(l, rowOver, columnCount-1);
					isHighLight = false ;
				}
				*/
				if(!isHighLight){
					isHighLight = true ;
//					rowOver = row ;
//					table.setValueAt(h, row, columnCount-1);
					table.setCursor(new Cursor(Cursor.HAND_CURSOR)) ;
				}
			} else {
				if(isHighLight){
					table.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)) ;
					isHighLight = false ;
				}
			}
		} else {
			if(isHighLight){
//				table.setValueAt(l, rowOver, columnCount-1);
				table.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)) ;
				isHighLight = false ;
			}
		}
		
		
		
		
	}

}
